<template>
  <PermissionContainer
    :jiBieArr="[1, 2]"
    :type="1"
    :tipArr="[
      ,
      '积分功能只对集团和站点级别开放',
      '因为集团上关于积分的共享配置，此级没有配置权限',
      ,
      '您暂未启动积分功能',
    ]"
  >
    <div>
      <el-form :model="sumForm">
        <el-form-item label="开始积分功能">
          {{ sumForm.integralFlag == 0 ? "已关闭" : "已开启" }}
        </el-form-item>
        <el-form-item
          label="积分清空的日期"
          v-if="sumForm.integralTermSetting == 2"
        >
          {{
            sumForm.integralFlag == 0
              ? "积分永久有效"
              : sumForm.integralEmptyTime
          }}
        </el-form-item>
        <el-form-item
          label="所有积分规则的失效时间"
          v-if="sumForm.termDateManage == 2"
        >
          {{
            sumForm.termDateManage == 0 ? "积分规则永久有效" : sumForm.emptyDate
          }}
        </el-form-item>
        <el-form-item label="多倍积分活动的状态">
          {{
            sumForm.integralActivity == 0
              ? "未开启"
              : integralActivityDatePicker
          }}
        </el-form-item>
        <el-form-item label="正在生效的积分规则">
          {{ ruleList.length == 0 ? "当前尚没有正在生效的规则" : "" }}
          <el-table :data="ruleList">
            <af-table-column
              label="油站id"
              align="left"
              prop="stationId"
              v-if="false"
            />
            <af-table-column label="油品" align="left" prop="oilName" />
            <af-table-column label="会员等级" align="left">
              <template slot-scope="scope">
                <span>{{
                  scope.row.gradeName === null ? "所有人" : scope.row.gradeName
                }}</span>
              </template>
            </af-table-column>
            <af-table-column label="规则" align="left">
              <template slot-scope="scope">
                <span>{{
                  (!scope.row.ruleType
                    ? ""
                    : ruleTypeFormat[scope.row.ruleType] + "---") +
                  "满" +
                  scope.row.ruleTerms +
                  (!scope.row.ruleType
                    ? "元开始积分"
                    : ruleTermsFormat[scope.row.ruleType] + "---") +
                  "---每消费" +
                  scope.row.saleAmt +
                  "元获得" +
                  scope.row.integral +
                  "积分"
                }}</span>
              </template>
            </af-table-column>
          </el-table>
        </el-form-item>
      </el-form>
    </div>
  </PermissionContainer>
</template>

<script>
import { listRuleInfo, listRuleOne } from "@/api/integral/rule";
export default {
  name: "RuleList",
  data() {
    return {
      total: 0,
      ruleList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10000,
      },
      queryInfo: {},
      // 表单参数
      form: {
        itemsDetail: [{}],
        notOilList: [{}],
        imgFileList: [],
      },
      // 表单校验
      rules: {},
      ruleTypeFormat: [, "以订单实付累计", "以订单应付累计", "以加油升数累计"],
      ruleTermsFormat: [, "元开始积分", "元开始积分", "L开始积分"],
      datePickerFormat: ["", "每周指定日", "每月指定日", "固定活动日"],
      sumForm: {},

      timeSelect: [
        ,
        ["周天", "周一", "周二", "周三", "周四", "周五", "周六"],
        [
          "1号",
          "2号",
          "3号",
          "4号",
          "5号",
          "6号",
          "7号",
          "8号",
          "9号",
          "10号",
          "11号",
          "12号",
          "13号",
          "14号",
          "15号",
          "16号",
          "17号",
          "18号",
          "19号",
          "20号",
          "21号",
          "22号",
          "23号",
          "24号",
          "25号",
          "26号",
          "27号",
          "28号",
          "29号",
          "30号",
          "31号",
        ],
      ],
    };
  },
  computed: {
    integralActivityDatePicker() {
      if (!this.sumForm.datePickerTime) {
        return "";
      }
      if (this.sumForm.integralActivity == 0) {
        return "";
      }

      let timeString = "";
      const datePicker = this.sumForm.datePicker;

      if (datePicker == 1) {
        timeString += "每周的";
      }
      if (datePicker == 2) {
        timeString += "每月的";
      }
      if (datePicker == 3) {
        timeString += "指定的日期 ";
      }
      if (datePicker == 1 || datePicker == 2) {
        this.sumForm.datePickerTime.forEach((ele) => {
          timeString += this.timeSelect[datePicker][ele] + "、";
        });
      } else if (datePicker == 3) {
        timeString += this.sumForm.datePickerTime.toString();
      }

      timeString = timeString.replace(/(、)$/g, "") + " 启动双倍积分";
      return timeString;
    },
  },
  created() {
    // 5.20
    listRuleOne({
      pageNum: 1,
      pageSize: 100,
    }).then((response) => {
      this.sumForm = response.data;
      if (this.sumForm.datePicker == "1" || this.sumForm.datePicker == "2") {
        if (this.sumForm.datePickerTime.indexOf(",") == -1) {
          const temp = [parseFloat(this.sumForm.datePickerTime)];
          this.sumForm.datePickerTime = temp;
          return;
        }
        this.sumForm.datePickerTime = this.sumForm.datePickerTime
          .split(",")
          .map(parseFloat)
          .sort((a, b) => {
            return a - b;
          });
      } else if (this.sumForm.datePicker == "3") {
        this.sumForm.datePickerTime = this.sumForm.datePickerTime.split(",");
      }
    });

    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      //积分规则信息
      listRuleInfo(this.queryParams).then((response) => {
        this.ruleList = response.rows;
        this.total = response.total;
      });
    },
    // termDateManageFormat(row, column) {
    //   if (row.termDateManage === "1") {
    //     return "永久";
    //   } else if (row.termDateManage === "2") {
    //     return "指定结束时间";
    //   }
    // },
    integralActivityFormat(row, column) {
      if (row.integralActivity === "1") {
        return "开启";
      } else if (row.integralActivity === "0") {
        return "关闭";
      }
    },
    // datePickerFormat(row, column) {
    //   if (row.datePicker === "1") {
    //     return "每周指定日";
    //   } else if (row.datePicker === "2") {
    //     return "每月指定日";
    //   } else if (row.datePicker === "3") {
    //     return "固定活动日";
    //   }
    // },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
  },
};
</script>
